<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Teacher;
use App\Visitor;
use DB;
use Config;
class VisitorController extends Controller
{
	public function searchAllTeacher(){
		// 获取教师
    	$name = Teacher::pluck("name");
    	return Reply($name, 200);
	}
	
	//搜索教师姓名关键字
	public function searchVisitor($id){
		if(isset($id)){
			$Teacher = new Teacher;
	    	$TeacherName=$Teacher->where('name','like','%'.$id.'%')->get();
	    	$names = array();
	    	foreach ($TeacherName as $key => $value) {
	    		$names[] = $value['name'];
	    	}
	    	return Reply($names, 200);
		}else{
			return Reply(array("message" => "非法操作"), 400);
		}
		

    	
	}

	// 存入
	public function saveVisitor(Request $request){
    	$name = $request->input('name');
    	if(!$name){
	      return Reply(array("message" => "请输入你的姓名"), 400);
	    }
    	$phone = $request->input('phone');
    	if(!$phone){
	      return Reply(array("message" => "请输入电话号码"), 400);
	    }
	    if(!is_mobile($phone))
	    {
	      return Reply(array("message" => "请输入正确手机号码"), 400);
	    }
    	$reason = $request->input('reason');
    	if(!$reason){
	      return Reply(array("message" => "请选择来访原因"), 400);
	    }
    	$people = $request->input('people');
    	if(!$people){
	      return Reply(array("message" => "请输入接待老师"), 400);
	    }
    	$openID = $request->input('openID');	
    	// 备注选填
    	$remake = $request->input('remake');
    	if(!$remake){
	      $remake = '';
	    }
    	// 存入
		$Visitor = new Visitor;
		$Visitor->phone = $phone;
	    $Visitor->openID = $openID;
	    $Visitor->name = $name;
	    $Visitor->reason = $reason;
	    $Visitor->reception = $people;
	    $Visitor->remake = $remake;
	    $Visitor->save_Time = date("Y-m-d H:i:s",time());
	    $Visitor->status = 0;
	    $Visitor->state = 1;
	    $result = $Visitor->save();
	    $idd = $Visitor->id;
	    if(!$result){
		      return Reply(array("message" => "访客登记失败"), 400);
		    }else{
		      return Reply($idd, 200);
		    }


		}

	public function visitorList(Request $request){
		// 获取要openID
		$openID = $request->input('openID');
		if($openID){
			$TeacherName = Teacher::where('openID',$openID)->value('name');
			$Visitor = new Visitor;
	    	$result = Visitor::where('openID',$openID)->where('state',1)->orWhere(function ($Visitor) use ($TeacherName) {
                $Visitor->where('reception','=',$TeacherName)->where('state','=',1);
            })->get();
	    	if($result){
	    		return Reply($result, 200);
	    	}else{
	    		return Reply(array("message" => "获取列表失败，请稍后重试"), 400);
	    	}
	    	
		}else{
			return Reply(array("message" => "非法操作"), 400);
		}

	}

	// 详情
	public function visitorView(Request $request){
		$id = $request->input('visitorID');
		if($id){
			$result = Visitor::find($id);
			return Reply($result, 200);
		}else{
			return Reply(array("message" => "非法操作"), 400);
		}
	}

	//  操作
	public function visitorOperate(Request $request){
		$id = $request->input('id');
		$operate = $request->input('operate');
		$Visitor = new Visitor;
		if(!$operate){
	      return Reply(array("message" => "请选择操作"), 400);
	    }
	    $time = date("Y-m-d H:i:s",time());
	    // 判断，修改状态
	    if( $operate === '同意访问'){
	        $result = Visitor::where('id', $id)->update(array(
	        	"status" => 1,"accept_time" => $time
	        ));
	      	return Reply(array("message" => "审批操作已完成"), 200);
	    }elseif($operate === '拒绝访问'){
	    	$result = Visitor::where('id', $id)->update(array(
	      		"status" => 2
	    	));
	      	return Reply(array("message" => "审批操作已完成"), 200);
	    }elseif($operate === '结束离校'){
	    	$result = Visitor::where('id', $id)->update(array(
	      		"status" => 3,"leave_time" => $time
	    	));
	      	return Reply(array("message" => "审批操作已完成"), 200);
	    }elseif($operate === '撤销申请'){
	    	$result = Visitor::where('id', $id)->update(array(
	      		"state" => 0
	    	));
	      	return Reply(array("message" => "审批操作已完成"), 200);
	    }

	}

	public function VisitorCounts(Request $request){
		$result=Visitor::where('state',1)->count();
		return Reply($result, 200);
	}
	//后台访客列表
	public function adminvisitorList1(Request $request){
		$Visitor = new Visitor;
		$result['data']=$Visitor->where('state',1)->orderby('ID', 'DESC')->get();
		$result['count']=$Visitor->where('state',1)->count();
		return Reply($result, 200);
	}
	//后台搜索
	public function visitorsearch(Request $request){
		$Visitor = new Visitor;	
		// 接收前端传来的访客搜索条件
        $visitor_status = $request->input("visitor_status");
        $visitor_name = $request->input("visitor_name");
        $visitor_reason = $request->input("visitor_reason");
        $time = $request->input("time");
        $where[] = array('state', 1);

		 // 访客教师姓名
        if (!empty($visitor_name)) {
            $where[] = array('reception', $visitor_name);
        }
		
		// 访客状态
        if (!empty($visitor_status)) {
            $where[] = array('status', $visitor_status);
        }
		
		  // 访客事由
        if (!empty($visitor_reason)) {
            $where[] = array('reason', $visitor_reason);
        }

        $ret = $Visitor->where($where)->orderBy('id', 'asc')->get();
        return Reply($ret, 200);
	}
	// 后台访客结束
	public function adminvisitorFinish($id){
		if(isset($id)){
			$time = date("Y-m-d H:i:s",time());
		 	$result = Visitor::where('id', $id)->update(array(
      			"status" => 3,"leave_time" => $time
	    	));
	      	return Reply(array("message" => "操作成功"), 200);
		}else{
			return Reply(array("message" => "非法操作"), 400);
		}
	     
	}

	public function delVisitor($id){
		if(isset($id)){
		 	$result = Visitor::where('id', $id)->update(array(
      			"state" => 0
	    	));
	      	return Reply(array("message" => "操作成功"), 200);
		}else{
			return Reply(array("message" => "非法操作"), 400);
		}
	     
	}

	public function pageVisitor(Request $request){
        // 获取数据
        $status = $request->input('status');
        $name = $request->input('name');
        $limit = $request->input('limit');
        $offset = ($request->input('offset') - 1) * $limit;
    	$where[] = array('state', 1);
        if ($status != null) {
            $where[] = array('status', $status);
        }
        if (!empty($name)) {
            $where[] = array('reception', $name);
        }
        if ($limit != null || $offset != null) {
            $result = Visitor::where($where)->offset($offset)->limit($limit)->orderby('ID', 'DESC')->get();
            if($result){
                return $result;
            }else{
                return Reply(array("message" => "获取列表失败"), 400);
            }
        }else{
        	$result = Visitor::where($where)->orderby('ID', 'DESC')->get();
        	if($result){
                return $result;
            }else{
                return Reply(array("message" => "获取列表失败"), 400);
            }
        }
  
    }
 

}